from typing import Any

from aiogram import types
from aiogram_dialog import DialogManager
from arq import ArqRedis
from fluentogram import TranslatorRunner

from weed_shop.core.models.enums.loyverse_sync import SyncType
from weed_shop.tg_bot.services.tg_helpers import answer_msg_with_autodelete


async def start_loyverse_sync(call: types.CallbackQuery, widget: Any, manager: DialogManager):
    arqredis: ArqRedis = manager.middleware_data.get("arqredis")
    locale: TranslatorRunner = manager.middleware_data.get("locale")

    if widget.widget_id == "bot_to_loyverse":
        sync_type = SyncType.bot_to_loyverse
    else:
        sync_type = SyncType.loyverse_to_bot

    await arqredis.enqueue_job(
        "loyverse_sync",
        user_tg_id=call.from_user.id,
        sync_type=sync_type,
    )
    await answer_msg_with_autodelete(call.message, locale.get("sync-started"))
    await manager.done()
